package com.yunfei.api.v2.service;

import com.google.gson.Gson;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.contants.AppKeyContant;
import com.yunfei.api.v2.enums.CancelElecHtEnum;
import com.yunfei.api.v2.param.ContractIdParam;
import com.yunfei.api.v2.result.LookHeTongSignStatusJson.LookHeTongSignStatusJson;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.utils.http.HttpIOUtil;
import com.yunfei.utils.http.QueryHeTongDetailHttpUtil;
import com.yunfei.utils.string.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class YunContractGetStatusService {
    @Autowired
    private Environment env;
    /**
    * @Description: 云合同查看合同状态
    * @Author: zhengyunfei
    * @Date: 2019/8/27
    * @time:11:03
    */
    public ResultModel getStatus(ContractIdParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        String appId=env.getProperty(AppKeyContant.appId);
        String appKey=env.getProperty(AppKeyContant.appKey);
        try{
            String contractId=param.getContractId();//合同id
            if(StringUtil.isNullOrEmpty(contractId)){
                resultModel.setCode(CancelElecHtEnum.CONTRACT_ID_NOTNULL.getCode());
                resultModel.setMessage(CancelElecHtEnum.CONTRACT_ID_NOTNULL.getMessage());
                return resultModel;
            }
            HttpIOUtil httpIOUtil=new HttpIOUtil();
            String token= httpIOUtil.getLongToken(appId,appKey,"");
            String url= YunHeTongApiContant.DOMAIN+"/contract/status/0/"+contractId;
            JSONObject jsonObject= QueryHeTongDetailHttpUtil.get(url,token);
            Gson gson=new Gson();
            LookHeTongSignStatusJson resultJson= (LookHeTongSignStatusJson) gson.fromJson(jsonObject.toString(),LookHeTongSignStatusJson.class);
            //LookHeTongSignStatusJson resultJson= (LookHeTongSignStatusJson) JSONObject.toBean(jsonObject,LookHeTongSignStatusJson.class);
            resultModel.setData(resultJson);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
}
